<!DOCTYPE html>
<html lang="en">
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('集合列表')" />
</head>
<body class="gray_bg">
    <div class="container-div">
        <div class="btn-group-sm" id="toolbar">
            <a class="btn btn-success" onclick="selectBook()" >
                <i class="fa fa-plus"></i> 添加书本
            </a>
        </div>
        <div class="row">
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

</body>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
   
    var prefix = ctx + "book/collection/book";
    var collection_id = [[${collection_id}]];
    $(function(){

            var options = {
                modalName:"合集",
                url:prefix +"/list?collection_id="+collection_id,
                createUrl:prefix+"/add",

                removeUrl:prefix+"/remove/{id}",
                uniqueId:"id",
                columns:[
                {
                        field:"id",
                        title:"id"
                    },
                    {
                        field:"name",
                        title:"name"
                    },
                    {
		            title: '操作',
		            align: 'center',
		          formatter: function(value, row, index) {
                        var actions = [];
                        let id = collection_id+"_"+row.id;


                        actions.push('<a class="btn btn-danger btn-xs ' +  '" href="javascript:void(0)" onclick="removeBookFromCollection(\'' + id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
		        }
                ]

            };
            $.table.init(options);

    });
    function selectBook() {
        $.modal.open("select book","/book/manage",1000,500,function(index,layero){
            var iframeWin = layero.find('iframe')[0];
            let selected = iframeWin.contentWindow.getSelections();
            console.log("call back:");
            console.log(selected);
            $.modal.close(index);
            let ids = [];
            for(let item of selected ) {
                ids.push(item.id);
            }
            addBookToCollection(ids);

        });
    }
    function addBookToCollection(ids) {
            console.log("add book to collection:");
            console.log(ids)
            $.operate.post(prefix,{
                collection_id:collection_id,
                ids:ids.join()
            },function(){

            });
    }
    function removeBookFromCollection(id){
    $.operate.post(prefix+"/remove",{
                compoundId:id
            },function(){
            console.log("refresh..");

            });
    }


</script>
</html>